package DataStructure.arrayRelevant.matrix;

import Common.Utils.UTFactory;
import org.junit.Test;

/**
 * @author 蔚蔚樱
 * @version 1.0
 * @date 2020/12/20
 * @author—Email micromicrohard@outlook.com
 * @blogURL https://blog.csdn.net/Micro_Micro_Hard
 * @description 细菌扩展
 * 二维矩阵中 "1" 表示一个细菌，每分钟，细菌会向上下左右扩展一次，
 * 问多少分钟后，整个二维数组全部被细菌覆盖满
 */
public class BacterialExpansion {

    @Test // 验证功能：用于全量测试
    public void TestFunc() throws Exception {
        UTFactory.FullTest(this.getClass());
    }

    @Test // 调试功能 : 用于复现错误测试案例
    public void DoubleTrack() throws Exception {
        String input = "";
        String output = "";
        UTFactory.DebugTest(this.getClass(), input, output);
    }

    public int ExpansionMethod(int[][] bacterial) {
        if (bacterial == null || bacterial.length == 0) {
            return 0;
        }
        int count = 0;
        while (!BacterialFullCheck(bacterial)) {
            count++;
            for (int row = 0; row < bacterial.length; row++) {
                for (int column = 0; column < bacterial[row].length; column++) {
                    if (bacterial[row][column] != count) { // 剪枝
                        continue;
                    }
                    set(bacterial, row + 1, column, count);
                    set(bacterial, row - 1, column, count);
                    set(bacterial, row, column + 1, count);
                    set(bacterial, row, column - 1, count);
                }
            }
        }
        return count;
    }

    public void set(int[][] bacterial, int row, int column, int flag) {
        if (bacterial == null || bacterial.length == 0 || row < 0 || column < 0 || row >= bacterial.length || column >= bacterial[row].length) {
            return;
        }
        if (bacterial[row][column] == 0) {
            bacterial[row][column] = flag + 1;
        }
    }

    public boolean BacterialFullCheck(int[][] bacterial) {
        for (int[] bRow : bacterial) {
            for (int b : bRow) {
                if (b == 0) { // 检测是否全0
                    return false;
                }
            }
        }
        return true;
    }
}
